package com.heima.article.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.heima.article.entity.ApArticle;
import com.heima.article.entity.ApAuthor;
import com.heima.article.mapper.ApAuthorMapper;
import com.heima.article.service.ApAuthorService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.exception.LeadException;
import com.heima.common.util.BeanHelper;
import com.heima.model.article.dtos.AuthorDto;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * APP文章作者信息表 服务实现类
 * </p>
 *
 * @author RMY
 * @since 2021-11-24
 */
@Service
public class ApAuthorServiceImpl extends ServiceImpl<ApAuthorMapper, ApAuthor> implements ApAuthorService {
        /**
         * 保存作者
         * @param authorDto
         * @return
         */
        @Override
        public Integer saveAuthor(AuthorDto authorDto) {
//        根据apUserId 查询作者信息
            QueryWrapper<ApAuthor> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(ApAuthor::getUserId,authorDto.getUserId());
            ApAuthor apAuthor = getOne(queryWrapper);
//        如果存在直接返回id
            if(apAuthor!= null){
                return apAuthor.getId();
            }
//        不存在就保存
            apAuthor = BeanHelper.copyProperties(authorDto,ApAuthor.class);
            apAuthor.setCreatedTime(new Date());
            save(apAuthor);
            return apAuthor.getId();
        }

    /**
     * 根据自媒体账号查找ApAuthor
     * @param wmUserId
     * @return
     */
    @Override
    public ApAuthor findByWmUserId(Integer wmUserId) {
        //新建查询条件
        QueryWrapper<ApAuthor> qw = new QueryWrapper<>();
        qw.lambda().eq(ApAuthor::getWmUserId,wmUserId);
        return getOne(qw);
    }
}
